package com.banyuan.课堂练习.IO流;

import java.io.*;

/*
 * 文件的切割；
 * 将aaa.txt平均切割成两个文件；
 * aaa_1.txt和aaa_2.txt;
 * */
public class Ex3 {
    public static void main(String[] args) {
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("aaa.txt"));
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("aaa_1.txt"));
             BufferedOutputStream bos2 = new BufferedOutputStream(new FileOutputStream("aaa_2.txt"))
        ) {
            int aaaNumHalf = bis.available() / 2;
            System.out.println(aaaNumHalf);
            byte[] arr = new byte[8];
            aaaNumHalf -= 8;
            while (aaaNumHalf >= 0) {
                bis.read(arr);
                bos.write(arr);
                bos.flush();
                aaaNumHalf -= 8;
            }

            bis.read(arr);
            bos.write(arr, 0, aaaNumHalf + 8);
            bos2.write(arr, aaaNumHalf + 8, -aaaNumHalf);

            int len;
            while ((len = bis.read(arr)) != -1) {
                bos2.write(arr, 0, len);
                bos2.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        try (FileInputStream in = new FileInputStream("aaa_1.txt");
             FileInputStream in2 = new FileInputStream("aaa_2.txt")) {
            System.out.println(in.available());
            System.out.println(in2.available());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
